/**
 * Created by karkarrotxu on 2016/11/8.
 */
qui(
    function (require, exports, module) {
        window.VODCom = window.VODCom || {
            version: '1.0'
        };
        VODCom.EventManager = function () {
            var eventIndex = 0;
            var managerArr = [];
            function bindEvent(eventType, func) {
                eventIndex++;
                var targetObj = {id: eventIndex, type: eventType, handler: function (paramsArray) {
                    func.apply(null, paramsArray)
                }}
                managerArr.push(targetObj);
                return targetObj.id;
            }

            function dispatchEvent(event) {
                $.each(managerArr, function (i, element) {
                    if (element.type == event.type) {
                        element.handler($.isArray(event.data) ? event.data : [event.data]);
                    }
                })
            }

            function removeBind(id) {
                var findTarget = null;
                $.each(managerArr, function (i, element) {
                    if (element.id == id) {
                        findTarget = element;
                    }
                })
                if (findTarget != null) {
                    var index = managerArr.indexOf(findTarget);
                    managerArr.splice(index, 1);
                }
            }

            return{
                bindEvent: bindEvent,
                dispatchEvent: dispatchEvent,
                removeBind: removeBind
            }
        }
    })
